package lueder.sqlutilities;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class MySqlConnection {
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	private static String RESOURCENAME = "java:comp/env/jdbc/klk";
	private InitialContext jndiCntx = null;
	private DataSource ds = null;

	public MySqlConnection() {
		try {
			jndiCntx = new InitialContext();
			ds = (DataSource) jndiCntx.lookup(RESOURCENAME);
		} catch (Exception e) {
		}

	}

	public ResultSet getResultsetFromSql(String sql) {
		try {
			conn = ds.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			/*if (rs != null)
				try {
					rs.close();
				} catch (Exception e) {
				}*/
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
				}
			if (conn != null)
				try {
					conn.close();
				} catch (Exception e) {
				}
		}
		return rs;
	}
}
